package cn.wonderland.org.controller;

import cn.wonderland.base.annotation.PreAuthorize;
import cn.wonderland.base.util.AjaxResult;
import cn.wonderland.base.util.PageList;
import cn.wonderland.org.domain.Department;
import cn.wonderland.org.query.DepartmentQuery;
import cn.wonderland.org.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

//    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
//    public List<Department> findAll(){
//        return departmentService.findAll();
//    }

    @GetMapping()
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department findById(@ApiParam(value="ID属性",required = true) @PathVariable("id") Long id){
        return departmentService.findById(id);
    }

    @PutMapping()
    public AjaxResult addOrUpdate(@RequestBody Department department){
        if(department.getId() != null){ // id不为空，执行修改
            departmentService.update(department);
        }else{ // id为空，执行新增
            departmentService.add(department);
        }
        return AjaxResult.me().success();
    }

    @DeleteMapping("/{id}")
    @PreAuthorize(name="部门删除部门",sn = "department:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        departmentService.delete(id);
        return AjaxResult.me().success();
    }

    @PostMapping()
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query){
        return departmentService.queryByPage(query);
    }

    // 批量删除
    @PatchMapping
    @PreAuthorize(name="部门批量删除",sn = "department:batchDelete")
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        departmentService.batchDelete(ids);
        return AjaxResult.me().success();
    }

    @GetMapping("/tree")
    @PreAuthorize(name="部门无线级数",sn = "department:deptTree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }
}